package com.king.zxing.app.fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.imageview.ShapeableImageView;
import com.king.zxing.app.R;
import com.king.zxing.app.domain.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//个人中心
public class PersonalCenterFragment extends Fragment {

    List<Setting> list = new ArrayList<>();


    @SuppressLint("SetTextI18n")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal_center, container, false);

        ShapeableImageView imageView = view.findViewById(R.id.avatar);
        TextView username = view.findViewById(R.id.username);
        TextView userid = view.findViewById(R.id.userid);

        User user = Objects.requireNonNull(requireActivity().getIntent().getExtras()).getParcelable("user");
        assert user != null;
        username.setText(user.username);
        userid.setText("ID: " + user.id);

        list.add(new Setting(R.drawable.icon_message, "个人信息", (View e) -> {
        }));
        list.add(new Setting(R.drawable.icon_collection, "我的收藏", (View e) -> {
        }));
        list.add(new Setting(R.drawable.icon_feedback, "问题反馈", (View e) -> {
        }));
        list.add(new Setting(R.drawable.icon_setting, "设置", (View e) -> {
        }));

        RecyclerView recyclerView = view.findViewById(R.id.recycler_set);
        recyclerView.setAdapter(new MyAdapter());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        return view;
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.item_list_settings, null);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            Setting item = list.get(position);
            holder.image.setImageResource(item.img_src);
            holder.name.setText(item.name);
            holder.layout.setOnClickListener(item.listener);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView name;

        ConstraintLayout layout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.set_icon);
            name = itemView.findViewById(R.id.set_textview);
            layout = itemView.findViewById(R.id.set_layout);
        }
    }

    private static class Setting {
        public final int img_src;
        public final String name;

        public final View.OnClickListener listener;

        public Setting(int img_src, String name, View.OnClickListener listener) {
            this.img_src = img_src;
            this.name = name;
            this.listener = listener;
        }
    }
}
